<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="ihis.model.*" %>
<%@ page import="ihis.core.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@page import="ihis.tms.client.IHISTMSClient"%>

<%@ include file="filter.jsp" %>
    
<%
	ScheduleReport report = (ScheduleReport) core.getReport(IHISConstants.REPORT_PERSONAL_SCHEDULE, filter);
	
	
	Schedule schedule = report.getSummary();
	if(schedule == null){
		schedule = new Schedule();
		schedule.setCompletedCnt(0);
		schedule.setDelayedCnt(0);
		schedule.setInProgressCnt(0);
		schedule.setPlannedCnt(0);
	}
%>


<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Project/Team/Personal Schedule Report</title>
    <link rel="stylesheet" href="script/jquery.mobile-1.0a4.1.min.css"/>
    <link rel="stylesheet" href="script/ihis.css"/>
    <script src="script/jquery-1.6.min.js"></script>
    <script src="script/ihis.schedule.js"></script>
    <script src="script/jquery.mobile-1.0a4.1.min.js"></script>
</head>

<body>
	<div id="schedulePage" data-role="page">
		<div data-role="header" data-position="fixed">
			<a href="#" data-icon="back" data-role="button" id="backButton">Back</a>
			<h1><%= IHISConstants.OWNERTYPE_PERSON == Integer.parseInt(ownerType) ? userName + "님의 일정" : "조직 일정" %></h1>
			<a href="#" data-icon="home" data-role="button" id="homeButton">Home</a>
		</div>
<%
	// 개인 일정인 경우
	if(IHISConstants.OWNERTYPE_PERSON == Integer.parseInt(ownerType)){
%>	
		<div data-role="content">
			<ul data-role="listview" data-inset="false">
				<% if(filter.getPeriodType() == IHISConstants.TIME_UNIT_MONTHLY){ %>
				<li data-role="list-divider">금월</li>
				<% }else if(filter.getPeriodType() == IHISConstants.TIME_UNIT_WEEKLY){ %>
				<li data-role="list-divider">금주</li>
				<% } %>
				
				<li>
					<a href="#" id="inProgress">
						<h3>진행(<%= schedule.getInProgressCnt() %>)</h3>
					</a>
				</li>
				<li>
					<a href="#" id="completed">
						<h3>완료(<%= schedule.getCompletedCnt() %>)</h3>
					</a>
				</li>
				<li>
					<a href="#" id="delayed">
						<h3>지연(<%= schedule.getDelayedCnt() %>)</h3>
					</a>
				</li>
				<li>
					<h3>계(<%= schedule.getCompletedCnt() + schedule.getInProgressCnt() + schedule.getDelayedCnt() %>)</h3>
				</li>
				<% if(filter.getPeriodType() == IHISConstants.TIME_UNIT_MONTHLY){ %>
				<li data-role="list-divider">차월</li>
				<% }else if(filter.getPeriodType() == IHISConstants.TIME_UNIT_WEEKLY){ %>
				<li data-role="list-divider">차주</li>
				<% } %>
				<li>
					<a href="#" id="planned">
						<h3>진행 예정(<%= schedule.getPlannedCnt() %>)</h3>
					</a>
				</li>
			</ul>
			<%
				StringBuilder sb = new StringBuilder("Filter : ");
				if(!IHISUtil.isEmpty(startDate)){
					sb.append(startDate).append(" ");
				}
				if(!IHISUtil.isEmpty(endDate)){
					sb.append("~").append(endDate).append(" ");
				}
				if(!IHISUtil.isEmpty(user)){
					sb.append("/ ").append(userName).append("(").append(user).append(") " );
				}
				if(!IHISUtil.isEmpty(team)){
					sb.append("/ ").append(teamName).append(" ");
				}
				if(!IHISUtil.isEmpty(project)){
					sb.append("/ ").append(projectName).append(" ");
				}
				
			%>
			<h5><%= sb.toString() %></h5>
		</div> 
<%
	} else { // 조직 일정인 경우  
		List<Schedule> schedules = report.getScheduleList();
%>	
		<div data-role="content">
			<ul data-role="listview" data-inset="false">
				<li data-role="list-divider">요약</li>
				<li>
					<a href="" id="summary"> 
						<h3>금주</h3>
						<p>
							진행(<%= schedule.getInProgressCnt() %>)
						    완료(<%= schedule.getCompletedCnt() %>)
							지연(<%= schedule.getDelayedCnt() %>)
							계(<%= schedule.getCompletedCnt() + schedule.getInProgressCnt() + schedule.getDelayedCnt() %>)
						</p>
					</a>
				</li>
				<li>
					<a href="" id="planned"> 
						<h3>차주</h3>
						<p>진행 예정(<%= schedule.getPlannedCnt() %>)</p>
					</a>
				</li>

<%
	for(Schedule s : schedules){
		String tempTeamName = s.getTeamId();
		if(!IHISUtil.isEmpty(tempTeamName)){
			tempTeamName = IHISUtil.nullCheck(tms.getTeamName(tempTeamName));
		}
		String name = tms.getProjectName(s.getProjectId()) + " | "	+ tempTeamName;
%>
				<li data-role="list-divider"><%=  name %></li>
				<li>
					<a href="" id="summary"> 
						<h3>금주</h3>
						<p>
							진행(<%= s.getInProgressCnt() %>)
							완료(<%= s.getCompletedCnt() %>)
							지연(<%= s.getDelayedCnt() %>)
							계(<%= s.getCompletedCnt() + s.getInProgressCnt() + s.getDelayedCnt() %>)
						</p>
					</a>
				</li>
				<li>
					<a href="" id="planned"> 
						<h3>차주</h3>
						<p>진행 예정(<%= s.getPlannedCnt() %>)</p>
					</a>
				</li>
<%
	}
%>
			</ul>
<%
	StringBuilder sb = new StringBuilder("Filter : ");
	if(!IHISUtil.isEmpty(startDate)){
		sb.append(startDate).append(" ");
	}
	if(!IHISUtil.isEmpty(endDate)){
		sb.append("~").append(endDate).append(" ");
	}
	if(!IHISUtil.isEmpty(user)){
		sb.append("/ ").append(userName).append("(").append(user).append(") " );
	}
	if(!IHISUtil.isEmpty(team)){
		sb.append("/ ").append(teamName).append(" ");
	}
	if(!IHISUtil.isEmpty(project)){
		sb.append("/ ").append(projectName).append(" ");
	}
	
%>
			<h5><%= sb.toString() %></h5>
		</div>

<%
	}
%>	

	<script type="text/javascript">
    	$(function(){
    		$("#searchButton").bind("click", function(){
				<% 
					StringBuilder url = new StringBuilder("searchSchedule.jsp?filterFor=schedule");
					url.append("&periodType=").append("" + filter.getPeriodType());
					url.append("&ownerType=").append("" + filter.getOwnerType());

					if(filter.getStartDate() != null){
						url.append("&").append("startDate=").append(new SimpleDateFormat("yyyy-MM-dd").format(filter.getStartDate()));
					}
					if(filter.getEndDate() != null){
						url.append("&").append("endDate=").append(new SimpleDateFormat("yyyy-MM-dd").format(filter.getEndDate()));
					}
					if(!IHISUtil.isEmpty(filter.getUserId())){
						url.append("&").append("user=").append(filter.getUserId());
					}
					if(!IHISUtil.isEmpty(filter.getProjectId())){
						url.append("&").append("project=").append(filter.getProjectId());
					}
					if(!IHISUtil.isEmpty(filter.getTeamId())){
						url.append("&").append("team=").append(filter.getTeamId());
					}
				%>
				var url = "<%= url %>";
				document.location = url;
    		});
    	});
    	// 1. page 로 지정된 ID 값,  2.효과 3. 방향 :  false(>), true(<)
    </script>		
		
		<div data-role="footer" data-position="fixed">
			<a href="" id="searchButton" data-icon="check"  data-transition="slideup">Search</a>
			<a href="javascript:window.location.reload(true)" data-icon="refresh" data-rel="refresh" data-transition="flip">Refresh</a>
		</div>
		
	</div>
	
	<!-- 검색 정보를 저장해둔다. : Task 목록을 위해 사용 -->
	<div>
		<form action="#" method="post" name="filterForm">
			<input type="hidden" name="startDate" value="<%= filter.getReportDate()%>"/>
			<input type="hidden" name="endDate" value="<%= endDate%>"/>
			<input type="hidden" name="reportDate" value="<%= reportDate%>"/>
			<input type="hidden" name="periodType" value="<%= periodType%>"/>
			<input type="hidden" name="periodType" value="<%= periodType%>"/>
			<input type="hidden" name="user" value="<%= user%>"/>
			<input type="hidden" name="team" value="<%= team%>"/>
			<input type="hidden" name="ownerType" value="<%= ownerType%>"/>
			<input type="hidden" name="taskType" value=""/>
		</form>
	</div>
</body>
</html>